package com.jingwu.example.repo;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jingwu.example.mapper.AuthorMapper;
import com.jingwu.example.mapper.BookCategoryMapper;
import com.jingwu.example.mapper.CategoryMapper;
import com.jingwu.example.pojo.entity.AuthorDO;
import com.jingwu.example.pojo.entity.CategoryDO;
import com.jingwu.example.pojo.query.AuthorQuery;
import com.jingwu.example.pojo.query.CategoryQuery;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author jingwu
 * @since 2021/2/1
 */
@Repository
public class AuthorRepository {

    @Resource
    private AuthorMapper mapper;

    public Page<AuthorDO> selectPage(AuthorQuery query) {
        Page<AuthorDO> page = new Page<>(query.getIndex(), query.getSize());
        this.mapper.selectPage(page, toWrapper(query));
        return page;
    }

    public AuthorDO selectById(Integer id) {
        return this.mapper.selectById(id);
    }

    public List<AuthorDO> select(AuthorQuery query) {
        return this.mapper.selectList(toWrapper(query));
    }

    private LambdaQueryWrapper<AuthorDO> toWrapper(AuthorQuery query) {
        return new QueryWrapper<AuthorDO>()
                .lambda()
                .eq(StringUtils.isNoneBlank(query.getName()), AuthorDO::getName, query.getName())
                .eq(StringUtils.isNoneBlank(query.getSchool()), AuthorDO::getSchool, query.getSchool())
                .eq(StringUtils.isNoneBlank(query.getTitle()), AuthorDO::getTitle, query.getTitle());
    }

    public int insert(AuthorDO entity) {
        return this.mapper.insert(entity);
    }

    public boolean update(AuthorDO entity) {
        return this.mapper.updateById(entity) > 0;
    }
}
